package cn.jbolt.ec.rush;

import cn.jbolt._admin.permission.CheckPermission;
import cn.jbolt._admin.permission.PermissionKey;
import cn.jbolt.base.JBoltBaseController;
import cn.jbolt.common.model.BecRushstoreCommodity;
import cn.jbolt.ec.commodity.CommodityOptionService;
import cn.jbolt.ec.commodity.CommodityService;
import cn.jbolt.ec.retailer.RetailerService;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Kv;

@CheckPermission(PermissionKey.RETAILER_ADMIN)
public class EcRushCommodityController extends JBoltBaseController {
    @Inject
    RushCommodityService commodityService;

    @Inject
    RetailerService retailerService;
    @Inject
    CommodityService retailerCommodityservice;
    @Inject
    CommodityOptionService optionService;


    public void get_rush_commodities_for_all(int retailer_id){

            renderJson(commodityService.get_rush_commodities_for_all(retailer_id));
    }


    public void get_rush_commodities_for_spreader(int retailer_id){

            renderJson(commodityService.get_rush_commodities_for_spreader(retailer_id));
    }

    /**
     * 全民秒杀订单信息
     * @param client_id
     * @param type
     */
    public void rush_order_info(int client_id,int type){
        if (client_id>0&&type>0){
            renderJson(commodityService.rush_order_info(client_id, type));
        }else {
            renderText("参数异常");
        }

    }

    /**
     * 全民秒杀订单商品信息
     * @param charge_id
     */
    public void rush_all_commodity(Integer charge_id) {
        if (charge_id!=null&&charge_id>=0){
            renderJson(commodityService.rush_all_commodity(charge_id));
        }

    }

    /**
     * 商家获取秒杀商品订单
     * @param retailer_id
     * @param type
     */
    public void retailer_rush_order_info(int retailer_id,int type){
        if (retailer_id>0&&type>0) renderJson(commodityService.retailer_rush_order_info(retailer_id, type));
        else renderText("参数异常");
    }

    public void rush_expired(){
        renderJson(commodityService.rush_expired());
    }


    /**********************************************
     *  以下为  秒杀商品 后台管理方法
     * @author  heqingyu
     * ****************************************
     */

    /**
     *  后台管理主页面
     */
    @CheckPermission(PermissionKey.RUSH_COMMODITY_INDEX)
    public void index(){
        set("pageData",commodityService.get_rush_commodities_page(getPageNumber(),getPageSize(),getKeywords(),getInt("retailerId")));
        keepPara();
        render("index.html");
    }


    /**
     * 编辑 秒杀商品  价格和库存 信息
     */
    @CheckPermission(PermissionKey.RUSH_COMMODITY_MODIFY_PRICE_STOCK)
    public void modify_price_stock(){
        set("rushCommodity",commodityService.findById(getInt(0)));
        render("modify_price_stock_form.html");
    }

    /**
     * 更新 秒杀商品  价格和库存信息
     */
    public void update_price_stock(){
        renderJson(commodityService.update_price_stock(getModel(BecRushstoreCommodity.class,"rushCommodity"),get("credit_price"),get("rush_price"),getInt("old_credit_stock"),getInt("old_rush_stock"),getSessionAdminUserId()));
    }


    /**
     * 新增 秒杀商品
     */
    @CheckPermission(PermissionKey.RUSH_COMMODITY_ADD)
    public void add(){
        render("add_form.html");
    }


    /**
     * 保存 新增 秒杀商品
     */
    public void save(){
        renderJson(commodityService.save(getModel(BecRushstoreCommodity.class,"rushCommodity"),get("credit_price"),get("rush_price"),getSessionAdminUserId()));
    }


    /**
     * 删除 秒杀商品
     */
    @CheckPermission(PermissionKey.RUSH_COMMODITY_DELETE)
    public void delete_rush_commodity(){
        renderJson(commodityService.delete_rush_commodity(getInt(0)));
    }


    /**
     * 切换启用 是否需要会员资格
     */
    public void toggle_member_required(){
        renderJson(commodityService.toggle_member_required(getInt(0)));
    }




    /**
     *  秒杀商品 添加时  一级选项数据源 选择商家
     *
     */
    @CheckPermission(PermissionKey.RUSH_COMMODITY_ADD)
    public void rush_commodity_select_retailer(){
        renderJsonData(retailerService.getCommonList("id,name",Kv.by("status",0)));
    }


    /**
     * 秒杀商品 添加时  二级选项数据源 选择商品
     */
    @CheckPermission(PermissionKey.RUSH_COMMODITY_ADD)
    public void rush_commodity_select_comodity(){
        renderJsonData(retailerCommodityservice.getCommonList("id,name", Kv.by("retailer_id",getInt("retailer_id"))));
    }

    /**
     * 秒杀商品 添加时  三级选项数据源 选择商品规格
     */
    @CheckPermission(PermissionKey.RUSH_COMMODITY_ADD)
    public void rush_commodity_select_option(){
        renderJsonData(optionService. getCommonList("id,title",Kv.by("commodity_id",getInt("commodity_id"))));
    }


}
